package org.eae.eldf.webapp.controller.module;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eae.eldf.dto.module.ModuleRoleDto;
import org.eae.eldf.dto.module.ModuleRoleQueryDto;
import org.eae.eldf.entity.module.ModuleInfo;
import org.eae.eldf.entity.module.ModulePurview;
import org.eae.eldf.entity.module.ModuleRole;
import org.eae.eldf.framework.annotation.Token;
import org.eae.eldf.framework.pager.Pagination;
import org.eae.eldf.framework.persistence.Criteria;
import org.eae.eldf.framework.spring.SpringSecurityUtils;
import org.eae.eldf.service.module.ModuleInfoService;
import org.eae.eldf.service.module.ModuleRoleService;
import org.eae.eldf.webapp.base.AbstractController;
import org.eae.eldf.webapp.common.bean.Result;
import org.eae.eldf.webapp.common.bean.ResultList;
import org.eae.eldf.webapp.common.constant.StateDefine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContext;

@Controller
@RequestMapping("/module")
public class ModuleRoleController extends AbstractController<ModuleRoleDto> {

	private ModuleRoleService moduleRoleService;

	private ModuleInfoService moduleInfoService;

	@Autowired
	public void setModuleInfoService(ModuleInfoService moduleInfoService) {
		this.moduleInfoService = moduleInfoService;
	}

	@Autowired
	public void setModuleRoleService(ModuleRoleService moduleRoleService) {
		this.moduleRoleService = moduleRoleService;
	}

	/**
	 * 查询列表
	 */
	@RequestMapping(value = "/role/list")
	public String initPage(ModelMap modelMap, HttpServletRequest request,
			@RequestParam(required = false) String searchValue, @RequestParam(required = false) String searchValueInfo,
			@RequestParam(required = false) Long id, HttpServletResponse response) {

		// 查询moduleInfo 的名称
		List<ModuleInfo> moduleInfoList = moduleInfoService
				.queryList(Criteria.create(ModuleInfo.class).where("moduleIsDelete", new Object[] { 0 }));

		/*
		 * Pagination<ModuleRoleDto> pagination= this.initPager(request,
		 * response); pagination=moduleRoleService.pageModuleList(pagination,
		 * searchValue, searchValueInfo);
		 * 
		 * 
		 * modelMap.addAttribute("list",pagination);
		 * modelMap.addAttribute("searchValue", searchValue);
		 * modelMap.addAttribute("searchValueInfo",searchValueInfo);
		 */
		modelMap.addAttribute("listModuleInfos", moduleInfoList); // 模块信息Id
		// return "/module/role/list";
		return "/module/role/module_role";
	}

	@RequestMapping(value = "/role/rolelist")
	public @ResponseBody ResultList<ModuleRoleDto> roleList(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute(value = "moduleRoleQueryDto") ModuleRoleQueryDto moduleRoleQueryDto) {
		Pagination<ModuleRoleDto> pager = super.newPaginationInstance(moduleRoleQueryDto);
		pager = moduleRoleService.pageModuleRoleList(pager, moduleRoleQueryDto);

		return outPageArrayList(pager, moduleRoleQueryDto);
	}

	/**
	 * 新增角色
	 * 
	 * @param role
	 * @return
	 */
	@RequestMapping(value = "/role/add") // 判断module/role 的module_role_id
	@ResponseBody
	public Result add(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute(value = "moduleRole") ModuleRole moduleRole) {
		Result result = new Result(StateDefine.FLAG_SUCCESS, true);
		//查询角色名是否重复 若重复 给出提示 "角色名已经存在"
    	if (moduleRoleService.queryCount(Criteria.create(ModuleRole.class).where("roleName", new Object[]{moduleRole.getRoleName()}))>0) {
    		RequestContext context=new RequestContext(request);
    		result.setResultValue(false);
			result.setMessage(context.getMessage("role.msg.srolename.exist"));
		}
    	else {
    		moduleRoleService.insertModuleRole(moduleRole);
    		
    	}
		return result;

	}

	/**
	 * 编辑模块角色
	 * 
	 * @Date : 2016年10月24日
	 * @param request
	 * @param response
	 * @param moduleRole
	 * @return
	 */
	@RequestMapping(value = "/role/update")
	@ResponseBody
	public Result update(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute(value = "moduleRole") ModuleRole moduleRole) {
		Result result = new Result(StateDefine.FLAG_SUCCESS, true);
		moduleRoleService.updateModuleRole(moduleRole);
//		moduleRoleService.update(Criteria.create(ModuleRole.class).set("moduleInfoId", moduleRole.getModuleInfoId())
//				.set("roleName", moduleRole.getRoleName()).set("roleDesc", moduleRole.getRoleDesc())
//				.set("isDelete", moduleRole.getIsDelete()).set("lastUpdateTime", new Date())
//				.set("lastUpdateUser", SpringSecurityUtils.getCurrentUserName())
//				.set("isDefaultRole", moduleRole.getIsDefaultRole())
//				.set("isDelete", 0)
//				.where("moduleRoleId", new Object[] { moduleRole.getModuleRoleId() }));
		return result;

	}

    /**
     *  删除角色
     */
    @RequestMapping(value="/role/del")
    public @ResponseBody Result delete(@RequestParam Long id){
    	Result result = new Result(StateDefine.FLAG_SUCCESS, true);
    	try {
    		moduleRoleService.deleteByComRoleId(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return result;
    }
    
}
